import React, {Component} from 'react'
import PropTypes from 'prop-types'

export default class Footer extends Component {
  static propTypes = {
    todos: PropTypes.array.isRequired,
    checkAllTodo: PropTypes.func.isRequired,
  }

  // 
  handleCheckAll = (event) => {
    this.props.checkAllTodo(event.target.checked)
  }
  render() {
    const {todos} = this.props
    // 计算已完成的个数
    // pre是上一次的返回值，第一次调用时，由于上一次没有返回值，所以pre=0
    // current是当前遍历数据的每一个值
    const doneCount = todos.reduce((pre, current) => {
      return pre + (current.done ? 1 : 0)
    }, 0)
    // 计算总数
    const total = todos && todos.length

    return (
      <div className="todo-footer">
        <label>
          <input type="checkbox" onChange={this.handleCheckAll} checked={total && total === doneCount ? true : false} />
        </label>
        <span>
          <span>已完成{doneCount}</span> / 全部{total}
        </span>
        <button className="btn btn-danger">删除已完成任务</button>
      </div>
    )
  }
}
